package com.example.service;

import com.example.mapper.PatientMapper;
import com.example.pojo.ConditionsQuery;
import com.example.pojo.Patient;
import com.example.pojo.Result;
import org.springframework.beans.PropertyAccessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PatientService {
    @Autowired
    private PatientMapper patientMapper;
    public Result patientAdd(Patient patient) {
        /**
         * 非空校验
         */
        if(!StringUtils.hasText(patient.getPname())){
            return new Result(100,"姓名不能为空",null);
        }
        if(patient.getAge()==null){
            return new Result(100,"年龄不能为空",null);
        }
        if(patient.getSex()==null){
            return new Result(100,"性别不能为空",null);
        }
        if(patient.getState()==null){
            return new Result(100,"状态不能为空",null);
        }
        patient.setEnter_time(new Date());

        int row =patientMapper.insert(patient);
        if(row!=1){
            return new Result(100,"修改失败",null);
        }

        return new Result(200,"操作成功",null);
    }

    public Result editPatient(Patient patient) {
        /**
         * 非空校验
         */
        if (patient.getId() == null) {
            return new Result(100,"没有找到id",null);
        }
        if(!StringUtils.hasText(patient.getPname())){
            return new Result(100,"姓名不能为空",null);
        }
        if(patient.getAge()==null){
            return new Result(100,"年龄不能为空",null);
        }
        if(patient.getSex()==null){
            return new Result(100,"性别不能为空",null);
        }
        if(patient.getState()==null){
            return new Result(100,"状态不能为空",null);
        }
        if(patient.getState()==2){
            //出院
           patient.setOut_time(new Date());

        }
        int row=patientMapper.update(patient);
        if (row!=1){
            return new Result(100,"修改失败",null);
        }

        return new Result(100,"操作成功",null);
    }

    public List<Patient> getPatientList(ConditionsQuery conditionsQuery) {
        return patientMapper.getPatientList(conditionsQuery);
    }

    public Result delPatientById(Integer id) {
        /**
         * 查询是否存在
         */
        Patient p=patientMapper.getPatientById(id);
        if(p==null){
            return new Result(100,"id不存在",null);
        }
        int row=patientMapper.delPatientById(id);
        if (row!=1){
            return new Result(100,"操作失败",null);
        }
        return new Result(200,"操作成功",null);
    }

    public Result delBatch(String ids) {
        if (!StringUtils.hasText(ids)){
            return new Result(100,"请至少选择一条记录",null);
        }

        String[] idStr =ids.split(",");

        List<Integer> idList=new ArrayList<>();

        for (int i=0; i<idStr.length;i++){
            idList.add(Integer.parseInt(idStr[i]));
        }
        int rows =patientMapper.delBatch(idList);
        if(rows!=idList.size()){
            return new Result(100,"操作失败",null);
        }
        return new Result(200,"删除成功",null);
    }
}
